<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\EntrustPermissionRoleRepository;
use HiCommon\Repository\EntrustRolesRepository;
use HiCommon\Repository\EntrustRoleUserRepository;

class RoleService extends BaseService
{

    /**
     * 获取管理后台的所有角色
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_roles_for_admin(){
        $arr_option = [
          'order'=>['key'=>'id','value'=>'asc'],
          'where'=>['model'=>'admin']
        ];
        $data = EntrustRolesRepository::base_get_all($arr_option)->toarray();
        return  hello_success('成功',$data);
    }

    /**
     * 获取商户后台的所有角色
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_roles_for_shop($n_shop_id){
        $arr_option = [
          'order'=>['key'=>'id','value'=>'asc'],
          'where'=>[
            'model'=>'shop',
            'shop_id'=>$n_shop_id,
          ]
        ];
        $data = EntrustRolesRepository::base_get_all($arr_option)->toarray();
        return  hello_success('成功',$data);
    }


    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        $data = EntrustRolesRepository::base_get_list();
        $data = list_page_data($data);
        return  hello_success('成功',$data);
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        //处理角色权限数据
        filter_input_data($arr_input);
        $arr_role_permission = $arr_input['role_permission'];
        array_pull($arr_input,'role_permission');
        $arr_data = [];
        if(!empty($arr_role_permission)){
            foreach ($arr_role_permission as $value)
            {
                $arr_data[] = ['permission_id'=>$value];
            }
        }

        //添加
        $res = EntrustRolesRepository::base_add_one($arr_input)
                                     ->role_permission()
                                     ->createMany($arr_data)
                                     ->toarray();
        //            if(empty($res)){
        //                return hello_error('添加失败');
        //            }

        return  hello_success('成功',$res);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        $n_id = intval($n_id);
        if($n_id <= 0) {
            hello_error('数据ID不能为空');
        }

        //查询
        $res = EntrustRolesRepository::base_find_one($n_id)->toarray();
        if(empty($res)){
            return hello_error('查询失败');
        }

        return  hello_success('查询成功',$res);
    }

    /**
     * 查询所有权限并标记当前角色选择的权限
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_role_permission(int $n_id)
    {
        $n_id = intval($n_id);
        if($n_id <= 0) {
            hello_error('数据ID不能为空');
        }

        //查询所有权限
        $arr_all_permission = cache_permission_for_admin();

        //查询角色权限
        $arr_option = [
          'where'=>['role_id'=>$n_id]
        ];
        $res = EntrustPermissionRoleRepository::base_get_all($arr_option)->toarray();
        $arr_role_permission = array_to_arr_ids($res,'permission_id');
        foreach ($arr_all_permission as $key=>$value)
        {
            if(in_array($value['id'],$arr_role_permission)){
                $arr_all_permission[$key]['is_checked'] = true;
            }
        }

        return  hello_success('查询成功',$arr_all_permission);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        filter_input_data($arr_input);
        $arr_input['id'] = intval($arr_input['id']);
        if ($arr_input['id'] <= 0) {
            return hello_error('数据id不能为空');
        }

        //处理角色权限数据
        $arr_role_permission = $arr_input['role_permission'];
        array_pull($arr_input,'role_permission');
        $arr_data = [];
        if(!empty($arr_role_permission)){
            foreach ($arr_role_permission as $value)
            {
                $arr_data[] = ['permission_id'=>$value,'role_id'=>$arr_input['id']];
            }
        }

        //修改角色信息
        $res = EntrustRolesRepository::base_edit_one($arr_input);
        if ($res === false) {
            return hello_error('操作失败');
        }

        //删除角色权限
        $arr_where = ['role_id'=>$arr_input['id']];
        $res = EntrustPermissionRoleRepository::base_delete_by_where($arr_where,true);
        if ($res === false) {
            return hello_error('操作失败1');
        }

        //添加新的角色权限
        $res = EntrustPermissionRoleRepository::base_add_many($arr_data);
        if ($res === false) {
            return hello_error('操作失败2');
        }

        clear_cache_for_shop();
        return hello_success('成功');
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input){
        $arr_input['id'] = (int)$arr_input['id'];
        if ($arr_input['id'] <= 0) {
            return hello_error('数据id不能为空');
        }

        $res = EntrustRolesRepository::base_edit_one($arr_input);
        if ($res === false) {
            return hello_error('操作失败');
        }
        return hello_success('成功');
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        if ($n_id <= 0) {
            return hello_error('数据id不能为空');
        }

        //查询角色下是否有用户
        $arr_where = ['role_id'=>$n_id];
        $res = EntrustRoleUserRepository::base_get_count_by_where($arr_where);
        if($res > 0){
            return hello_error('角色已有用户，不能直接删除');
        }

        //删除角色
        $res = EntrustRolesRepository::base_delete_one($n_id);
        if($res === false){
            return hello_error('操作失败');
        }
        return  hello_success('成功');
    }
}